/**
 * 
 * <p>
 * Title: HeroSelector.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2004 Sun Microsystems, Inc.
 * </p>
 * <p>
 * Company: Sun Microsystems, Inc
 * </p>
 * 
 * @author Jeff Kesselman
 * @version 1.0
 */
package com.worldwizards;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

import com.hero.Hero;
import com.hero.HeroDesigner;

/**
 * 
 * <p>
 * Title: HeroSelector.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2004 Sun Microsystems, Inc.
 * </p>
 * <p>
 * Company: Sun Microsystems, Inc
 * </p>
 * 
 * @author Jeff Kesselman
 * @version 1.0
 */
public class HeroSelector extends JDialog {
    List<Hero> selectedHeros = new LinkedList<Hero>();
    JList availableHeroList;
    JList participatingHeroList;
    List<HeroSelectorListener> listeners = new ArrayList<HeroSelectorListener>();

    public HeroSelector(JFrame parent) {
        super(parent, "Select Heros Participating In Combat");
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        JPanel westPanel = new JPanel();
        westPanel.setLayout(new BorderLayout());
        westPanel.add(new JLabel("Available Heroes"));
        availableHeroList = new JList(new DefaultListModel());
        westPanel.add(new JScrollPane(availableHeroList), BorderLayout.CENTER);
        cp.add(westPanel, BorderLayout.WEST);
        JPanel eastPanel = new JPanel();
        eastPanel.setLayout(new BorderLayout());
        eastPanel.add(new JLabel("Heros Participating"));
        participatingHeroList = new JList(new DefaultListModel());
        eastPanel.add(new JScrollPane(participatingHeroList),
                BorderLayout.CENTER);
        cp.add(eastPanel, BorderLayout.EAST);
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(7, 1));
        JButton selectHero = new JButton(" Add To Combat ");
        selectHero.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                addHero();
            }

        });
        westPanel.add(selectHero, BorderLayout.SOUTH);
        JButton unselectHero = new JButton(" Remove From Combat ");
        unselectHero.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                removeHero();
            }

        });
        eastPanel.add(unselectHero, BorderLayout.SOUTH);
        availableHeroList.setCellRenderer(new ListCellRenderer() {
            JLabel l = new JLabel();

            public Component getListCellRendererComponent(JList list,
                    Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                l.setOpaque(true);
                l.setText(((Hero) value).getCharacterName());
                if (isSelected) {
                    l.setBackground(Color.GRAY);
                } else {
                    l.setBackground(Color.WHITE);
                }
                return l;
            }

        });
        participatingHeroList.setCellRenderer(new ListCellRenderer() {
            JLabel l = new JLabel();

            public Component getListCellRendererComponent(JList list,
                    Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                l.setOpaque(true);
                l.setText(((HeroPlaytimeRecord) value).name);
                if (isSelected) {
                    l.setBackground(Color.GRAY);
                } else {
                    l.setBackground(Color.WHITE);
                }
                return l;
            }

        });

        westPanel.add(new JLabel("Available Heroes"), BorderLayout.NORTH);
        eastPanel.add(new JLabel("Heroes In Combat"), BorderLayout.NORTH);
        pack();
    }

    public void addListener(HeroSelectorListener l) {
        listeners.add(l);
    }

    /**
     * 
     */
    protected void removeHero() {
        DefaultListModel mdl = (DefaultListModel) participatingHeroList.getModel();
        Object[] selections = participatingHeroList.getSelectedValues();
        for (Object selection : selections) {
            HeroPlaytimeRecord selected = (HeroPlaytimeRecord) selection;
            if (selected != null) {
                mdl.removeElement(selected);
                fireHeroRemoved(selected);
            }
        }
    }

    /**
     * @param selected
     */
    private void fireHeroRemoved(HeroPlaytimeRecord rec) {
        for (HeroSelectorListener l : listeners) {
            l.heroRemoved(rec);
        }

    }

    /**
     * 
     */
    protected void addHero() {
        Object[] selections = availableHeroList.getSelectedValues();
        for (Object selection : selections) {
            Hero selectedHero = (Hero) selection;
            if (selectedHero != null) {
                DefaultListModel participatingMdl = (DefaultListModel) participatingHeroList.getModel();
                HeroDesigner hdesigner = HeroDesigner.getInstance();
                Hero oldActive = HeroDesigner.getActiveHero();
                hdesigner.setActiveHero(selectedHero);
                HeroPlaytimeRecord rec = new HeroPlaytimeRecord(selectedHero);
                participatingMdl.addElement(rec);
                hdesigner.setActiveHero(oldActive);
                fireHeroAdded(rec);
            }
        }
    }

    /**
     * @param rec
     */
    private void fireHeroAdded(HeroPlaytimeRecord rec) {
        for (HeroSelectorListener l : listeners) {
            l.heroAdded(rec);
        }

    }

    /**
     * @param openHeroes
     */
    public void showSelector(List<Hero> openHeroes, Set<HeroPlaytimeRecord> name) {
        DefaultListModel combattantMdl = (DefaultListModel) participatingHeroList.getModel();
        combattantMdl.clear();
        for (HeroPlaytimeRecord rec : name) {
            combattantMdl.addElement(rec);
        }
        DefaultListModel availableMdl = (DefaultListModel) availableHeroList.getModel();
        availableMdl.clear();
        for (Hero loadedHero : openHeroes) {
            availableMdl.addElement(loadedHero);
        }
        setVisible(true);
    }
}
